package com.sunye.rabbit_mq_1;


import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/rabbitMqController")
public class RabbitMqController {

    /**
     *
     * 解耦--订单系统和库存系统、异步--用户注册发送邮件和消息、削（xue）峰
     */

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * work工作模式 （一个生产者绑定多个消费者）
     */
    @RequestMapping("/directRabbitMsg")
    public String directRabbitMsg(){
        for (int i = 0; i < 10; i++) {
            Map map = new HashMap();
            map.put("msg","这里是一条消息");
            map.put("number",10-i);
            map.put("data", Arrays.asList("haha","2222","true"));
            rabbitTemplate.convertAndSend("workRoutingKey",map);//路由key
        }
        return "sucess";
    }


    /**
     * 广播模式(生产者将消息发送给交换机，交换机发送给每一个消费者)
     */
    @RequestMapping("/fanoutMsg")
    public String fanoutMsg(){
        Map map = new HashMap();
        map.put("msg","这里是广播模式");
        map.put("number",10);
        //发送交换机，路由可以为空因为发送消息到交换机上路由没啥用
        rabbitTemplate.convertAndSend("fanout.exchange","",map);
        return "sucess";
    }

    //***********常用*****
    /**
     * 路由模式(生产者将消息发送给交换机，交换机发送给每个路由一致的消费者)
     */
    @RequestMapping("/routeMsg")
    public String routeMsg(){
        Map map = new HashMap();
        map.put("msg","这里是infor路由模式");
        map.put("number",10);
        rabbitTemplate.convertAndSend("route.exchange","routeInforKey",map);
        map.clear();
        map.put("msg","这里是error路由模式");
        map.put("number",100);
        rabbitTemplate.convertAndSend("route.exchange","routeErrorKey",map);
        return "sucess";
    }



    /**
     * 动态路由(订阅)模式(生产者将消息发送给交换机，交换机发送给每个路由一致的消费者)
     */
    @RequestMapping("/topicMsg")
    public String topicMsg(){
        Map map = new HashMap();
        map.put("msg","这里是user动态路由模式");
        map.put("number",10);
        rabbitTemplate.convertAndSend("topic.exchange","user.save.add",map);
        map.clear();
        map.put("msg","这里是order动态路由模式");
        map.put("number",100);
        rabbitTemplate.convertAndSend("topic.exchange","product.save.add",map);
        return "sucess";
    }







}
